package com.maxd.modules.coupon.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.maxd.constants.URLConstants;
import com.maxd.error.Result;
import com.maxd.error.ResultUtil;
import com.maxd.modules.coupon.entity.CouponCard;
import com.maxd.modules.coupon.entity.GenerateCouponCard;
import com.maxd.utils.CardNoUtils;
import com.maxd.utils.DateUtil;
import com.maxd.utils.HttpClientUtil;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Auther: Laban:yubo_010@163.com
 * @Date: 2020/11/12 17:06
 * @Description:
 */
public class CouponCardUtil {

    /**
     * 生成卡券
     * @param generateCouponCard
     * @return
     */
    public List<CouponCard> randomGenerateCouponCard(GenerateCouponCard generateCouponCard) {
        //分转元
        BigDecimal yuan = new BigDecimal("100");

        List<CouponCard> couponCards = new ArrayList<>();
        //需要生成的数量
        int cardNumber = generateCouponCard.getCardNumber();
        //卡券的金额
        String cardMoney = String.valueOf(generateCouponCard.getCardMoney());
        //卡券类型
        String cardType = generateCouponCard.getCardType();
        //卡券名称
        String cardName = generateCouponCard.getCardName();
        for (int i = 0; i < cardNumber; i++) {
            CouponCard couponCard = new CouponCard();
            String generateShortUuid = CardNoUtils.generateShortUuid();
            couponCard.setCreateTime(new Date());
            couponCard.setCardId(generateShortUuid);//卡券ID 随机
            couponCard.setCardName(cardName);//卡券名称
            couponCard.setCardNo(generateShortUuid);//卡券卡号 随机数
            //couponCard.setCardQrcode(null);//卡券二维码
            BigDecimal cardMoney1 = new BigDecimal(cardMoney);
            couponCard.setCardMoney(cardMoney1.multiply(yuan).toString());//卡券金额 单位:分
            couponCard.setCardType(cardType);//卡券类型 1:用户 2:商户
            couponCard.setCardIsActivation("0");//是否被激活/兑换/使用(0:未使用 1:已使用)
            couponCards.add(couponCard);
        }
        return  couponCards;
    }

    /**
     * 参数校验
     * @param generateCouponCard
     * @return
     */
    public Result verificationParameter(GenerateCouponCard generateCouponCard){
        if(generateCouponCard==null){
            return  ResultUtil.error(601,"请输入生成卡片的参数");
        }
        if(generateCouponCard.getCardMoney()==null){
            return  ResultUtil.error(602,"请输入生成卡片的金额");
        }
        if(generateCouponCard.getCardNumber()==null){
            return  ResultUtil.error(603,"请输入生成卡片的数量");
        }
        if(generateCouponCard.getCardType()==null){
            return  ResultUtil.error(604,"请输入生成卡片的类型");
        }
        if(generateCouponCard.getCardName()==null){
            return  ResultUtil.error(605,"请输入生成卡片的名称");
        }
        return null;
    }


    /**
     * ""转null
     */
    public CouponCard copyNull(CouponCard couponCard){
        if("".equals(couponCard.getCardName())){
            couponCard.setCardName(null);
        }
        if("".equals(couponCard.getCardIsActivation())){
            couponCard.setCardIsActivation(null);
        }
        if("".equals(couponCard.getCardMoney())){
            couponCard.setCardMoney(null);
        }
        if("".equals(couponCard.getCardNo())){
            couponCard.setCardNo(null);
        }
        if("".equals(couponCard.getCardUserId())){
            couponCard.setCardUserId(null);
        }
        if("".equals(couponCard.getCardId())){
            couponCard.setCardId(null);
        }
        if("".equals(couponCard.getCardType())){
            couponCard.setCardType(null);
        }
        return  couponCard;
    }


    /**
     * 淘宝 根据商品id获取商品优惠券价格 返回的是元
     * json解析
     */
    public String getTaobaoProdCouponPrice(String prodId){
        String couponmoney =  null;
        try {
            String doGetStr =
                    HttpClientUtil.doGet(URLConstants.GET_TAOBAO_COMMODITY__COUPON_PRICE_URL + prodId, null);
            JSONObject jsonObject = JSON.parseObject(doGetStr);
            String data = jsonObject.getString("data");
            JSONObject jsonObject1 = JSON.parseObject(data);
            couponmoney = jsonObject1.getString("couponmoney");
        }catch (Exception e){
            return couponmoney;
        }
        return couponmoney;
    }

    /**
     * 拼多多 根据商品id获取商品优惠券价格 返回的是分
     */
    public String getPddProdCouponPrice(String prodId){
        String couponmoney =  null;
        try {
            String doGetStr =
                    HttpClientUtil.doGet(URLConstants.GET_PDD_COMMODITY__COUPON_PRICE_URL + prodId, null);
            JSONObject jsonObject = JSON.parseObject(doGetStr);
            String data = jsonObject.getString("goodsDetailResponse");
            JSONObject dataList = JSON.parseObject(data);
            JSONArray goodsDetails = dataList.getJSONArray("goodsDetails");
            couponmoney = goodsDetails.getJSONObject(0).getString("couponDiscount");
        }catch (Exception e){
            return couponmoney;
        }
        return couponmoney;
    }

    /**
     * 京东 根据商品id获取商品优惠券价格 返回单位是元
     * json解析
     */
    public String getJDProdCouponPrice(String prodId){
        String couponmoney =  null;
        try {
            String doGetStr =
                    HttpClientUtil.doGet(URLConstants.GET_JD_COMMODITY__COUPON_PRICE_URL + prodId, null);
            JSONObject jsonObject = JSON.parseObject(doGetStr);
            JSONObject data = jsonObject.getJSONObject("data");
            JSONArray list = data.getJSONArray("list");
            JSONObject jsonObject1 = list.getJSONObject(0);
            String couponInfo = jsonObject1.getString("couponInfo");
            JSONObject discount = JSON.parseObject(couponInfo);
            JSONArray couponList = discount.getJSONArray("couponList");
            couponmoney = couponList.getJSONObject(0).getString("discount");
        }catch (Exception e){
            return couponmoney;
        }
        return couponmoney;
    }


}
